package com.ln.property_control.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ln.property_control.mapper.TransferMapper;
import com.ln.property_control.pojo.*;
import com.ln.property_control.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘念
 * @create 2021-07-08-15:45
 */
@Service
public class TransferServiceImpl implements TransferService {


    @Autowired
    private TransferMapper transferMapper;

    @Override
    public Map<String, Object> selectTransferAll(Integer page,Integer limit) {
        Map<String,Object> map = new HashMap<>();
        Page<Object> objects = PageHelper.startPage(page, limit);
        List<TransferVo> transferVos = transferMapper.selectTransferAll();
        long total = objects.getTotal();
        map.put("count", total);
        map.put("data", transferVos);
        map.put("code", 0);
        return map;
    }

    //查看
    @Override
    public TransferVo selectByOrderNo(String transfer_order_no) {
        return transferMapper.selectByOrderNo(transfer_order_no);
    }

    @Override
    public List<PropertyAndTransferVo> selectByOrderNOProperty(String transfer_order_no) {
        return transferMapper.selectByOrderNOProperty(transfer_order_no);
    }

    @Override
    public List<Department> selectByParentId(int did) {
        return transferMapper.selectByParentId(did);
    }

    @Override
    public List<TransferType> selectTransferType() {
        return transferMapper.selectTransferType();
    }

    @Override
    public List<PropertyVo> selectPropertyByDid(int did) {
        return transferMapper.selectPropertyByDid(did);
    }

    @Override
    public int add(Transfer transfer, String[] property_id,String applicant,String transfer_order_no,int depar_id,int after_depar_id) {
        int count = transferMapper.AddTransfer(transfer);
        if(count>0){
            count = transferMapper.AddTransferAndProperty(transfer.getId(), property_id);
            count = transferMapper.deleteDepAndProByDepId(depar_id);
            count = transferMapper.addDepartmentAndProperty(after_depar_id, property_id);
            transferMapper.addAudit(applicant,transfer_order_no);

        }
        return count;
    }

    @Override
    public int updateStatus(int transfer_status, String transfer_order_no,String user_name) {
        int count = transferMapper.updateStatus(transfer_status, transfer_order_no);
        if(count>0){
            count = transferMapper.updateAuditStatus(transfer_status, transfer_order_no,user_name);
        }
        return count;
    }


    @Override
    public Map<String, Object> selectAudit() {
        Map<String,Object> map = new HashMap<>();
        List<AuditVo> audits = transferMapper.selectAudit();
        map.put("data", audits);
        map.put("code", 0);
        return map;
    }
}
